I also wrote a function that converts the craft model to mesh data, which you can use if you want
LoadObject = class()
function LoadObject:init(objPath,id)
self.e = scene:entity()
-- self.e.position = vec3(0,0,0)
local model = craft.model(objPath)
self.e:add(craft.renderer,model)
-- self.e.material = craft.material(asset.builtin.Materials.Standard)
-- self.e.material.map = readImage("Dropbox:face1")
self.e.active = true
self.id = id
self.m = self:model2mesh(self.e.model, self.id)
self.m:setColors(255,255,255,255)
self.m.texture = readImage(asset.builtin.Blocks.Ice)
---[[ ??OpenGLES3.0 ???9_1 ? shader
self.m.shader = shader(s.v3,s.f3)
-- m.shader = shader(s.v2,s.f2)
self.m.shader.modelMatrix = matrix()
self.m.shader.viewMatrix = matrix()
self.m.shader.projectionMatrix = matrix()
-- self.m.shader.modelViewProjection = matrix()
-- m.shader.modelMatrix=m.shader.modelMatrix:rotate(50, 0, 1, 0)
self.m.shader.uCamera = vec3(0,5,1)
self.m.shader.uLightLocation = vec3(100,103,103)
self.m.shader.sTexture = self.m.texture
end
function LoadObject:model2mesh(model,id)
local m = mesh()
local indices = model.indices
local vb = m:buffer("position")
local tb = m:buffer("texCoord")
local nb = m:buffer("normal")
local cb = m:buffer("color")
local vt,tt,nt,ct = {{}},{{}},{{}},{{}}
vb:resize(#indices)
tb:resize(#indices)
nb:resize(#indices)
cb:resize(#indices)
-- ???? id ??????????
local str = readText(asset.."Model"..id..".txt")
-- ????????????????????
if str == "" then
for k=1,#indices do
vb[k] = model.positions[model.indices[k]]
tb[k] = model.uvs[model.indices[k]]
nb[k] = model.normals[model.indices[k]]
cb[k] = model.colors[model.indices[k]]
-- ?vec3?vec2 ??????????????????
vt[k] = {vb[k].x, vb[k].y, vb[k].z}
tt[k] = {tb[k].x, tb[k].y}
nt[k] = {nb[k].x, nb[k].y, nb[k].z}
ct[k] = {cb[k].x, cb[k].y, cb[k].z}
end
local modelString = json.encode({vt,tt,nt,ct})
saveText(asset.."Model"..id..".txt", modelString)
else
local t = json.decode(str)
vt,tt,nt,ct = t[1], t[2], t[3], t[4]
-- print("t[3][2], t[1][1][1]", #t[3][2], t[1][1][1])
-- ????????????????
for k = 1,#vt do
vb[k] = vec3(vt[k][1], vt[k][2], vt[k][3])
tb[k] = vec2(tt[k][1], tt[k][2])
nb[k] = vec3(nt[k][1], nt[k][2], nt[k][3])
cb[k] = vec3(ct[k][1], ct[k][2], ct[k][3])
end
end
print("indices: ", #indices, type(id), id)
return m
end
function LoadObject:update(dt)
end
function LoadObject:drawSelf()
-- ?? mesh ??
self.m:draw()
end
s = {
v3 =[[#version 300 es
uniform mat4 modelViewProjection; //?????
uniform mat4 modelMatrix; //????
uniform mat4 viewMatrix; //????
uniform mat4 projectionMatrix; //????
uniform vec3 uLightLocation; //????
uniform vec3 uCamera; //?????
in vec3 position; //????
in vec3 normal; //?????
in vec2 texCoord; //??????
//?????????????
out vec4 ambient;
out vec4 diffuse;
out vec4 specular;
out vec2 vTextureCoord;
//??????????
void pointLight( //??????????
in vec3 iNormal, //???
inout vec4 ambient, //???????
inout vec4 diffuse, //???????
inout vec4 specular, //???????
in vec3 lightLocation, //????
in vec4 lightAmbient, //?????
in vec4 lightDiffuse, //?????
in vec4 lightSpecular //?????
){
ambient=lightAmbient; //????????????
vec3 normalTarget=position+iNormal; //?????????
vec3 newNormal=(modelMatrix*vec4(normalTarget,1)).xyz-(modelMatrix*vec4(position,1)).xyz;
newNormal=normalize(newNormal); //???????
//?????????????
vec3 eye= normalize(uCamera-(modelMatrix*vec4(position,1)).xyz);
//??????????????vp
vec3 vp= normalize(lightLocation-(modelMatrix*vec4(position,1)).xyz);
vp=normalize(vp);//???vp
vec3 halfVector=normalize(vp+eye); //??????????
float shininess=30.0; //?????????
float nDotViewPosition=max(0.0,dot(newNormal,vp)); //?????vp????0????
diffuse = lightDiffuse*nDotViewPosition; //??????????
float nDotViewHalfVector=dot(newNormal,halfVector); //?????????
float powerFactor=max(0.0,pow(nDotViewHalfVector,shininess)); //?????????
specular=lightSpecular*powerFactor; //??????????
}
void main()
{
// mat4 modelViewProject = projectionMatrix*(viewMatrix* modelMatrix);
gl_Position = modelViewProjection * vec4(position,1); //??????????????????
// ????????????????????
vec4 ambientTemp, diffuseTemp, specularTemp;
pointLight(normalize(normal),ambientTemp,diffuseTemp,specularTemp,uLightLocation,vec4(0.15,0.15,0.15,1.0),vec4(0.59,0.59,0.59,1.0),vec4(0.4,0.4,0.4,1.0));
ambient=ambientTemp;
diffuse=diffuseTemp;
specular=specularTemp;
vTextureCoord = texCoord;//????????????????
}
]],
f3 = [[#version 300 es
precision mediump float;
uniform sampler2D sTexture;//??????
//?????????????
in vec4 ambient;
in vec4 diffuse;
in vec4 specular;
in vec2 vTextureCoord;
out vec4 fragColor;
void main()
{
//???????????
vec4 finalColor=texture(sTexture, vTextureCoord);
//???????
fragColor = finalColor*ambient+finalColor*specular+finalColor*diffuse;
}
]],
}