mirror of
https://github.com/cocos/cocos4.git
synced 2026-12-05 02:52:39 +08:00
raycast
This commit is contained in:
+40
-8
@@ -124,6 +124,15 @@ function getIndexUnitStride (indexUnit: IndexUnit) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
function getIndexUnitCtor (indexUnit: IndexUnit) {
|
||||
switch (indexUnit) {
|
||||
case IndexUnit.UINT8: return Uint8Array;
|
||||
case IndexUnit.UINT16: return Uint16Array;
|
||||
case IndexUnit.UINT32: return Uint32Array;
|
||||
}
|
||||
return Uint8Array;
|
||||
}
|
||||
|
||||
export interface IVertexAttribute {
|
||||
/**
|
||||
* Attribute Name.
|
||||
@@ -222,6 +231,11 @@ export interface IPrimitive {
|
||||
*/
|
||||
vertexBundelIndices: number[];
|
||||
|
||||
/**
|
||||
* This primitive's topology.
|
||||
*/
|
||||
primitiveMode: GFXPrimitiveMode;
|
||||
|
||||
indices?: {
|
||||
/**
|
||||
* The indices data range of this primitive.
|
||||
@@ -234,10 +248,11 @@ export interface IPrimitive {
|
||||
indexUnit: IndexUnit;
|
||||
};
|
||||
|
||||
/**
|
||||
* This primitive's topology.
|
||||
*/
|
||||
primitiveMode: GFXPrimitiveMode;
|
||||
// for raycast purpose
|
||||
geomInfo?: {
|
||||
doubleSided?: boolean;
|
||||
range: IBufferRange;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -265,10 +280,17 @@ export interface IMeshStruct {
|
||||
maxPosition: Vec3;
|
||||
}
|
||||
|
||||
// for raycast purpose
|
||||
export interface IGeometricInfo {
|
||||
positions: Float32Array;
|
||||
indices: Uint16Array;
|
||||
doubleSided?: boolean;
|
||||
}
|
||||
|
||||
export interface IRenderingSubmesh {
|
||||
inputAssembler: GFXInputAssembler;
|
||||
primitiveMode: GFXPrimitiveMode;
|
||||
doubleSided?: boolean;
|
||||
geometricInfo?: IGeometricInfo;
|
||||
}
|
||||
|
||||
export class RenderingMesh {
|
||||
@@ -434,6 +456,7 @@ export class Mesh extends Asset {
|
||||
}
|
||||
|
||||
let indexBuffer: GFXBuffer | null = null;
|
||||
let ib: any = null;
|
||||
if (primitive.indices) {
|
||||
const indices = primitive.indices;
|
||||
|
||||
@@ -445,7 +468,9 @@ export class Mesh extends Asset {
|
||||
});
|
||||
indexBuffers.push(indexBuffer);
|
||||
|
||||
indexBuffer.update(new Uint8Array(buffer, indices.range.offset), 0, indices.range.length);
|
||||
ib = new (getIndexUnitCtor(indices.indexUnit))(buffer, indices.range.offset,
|
||||
indices.range.length / getIndexUnitStride(indices.indexUnit));
|
||||
indexBuffer.update(ib);
|
||||
}
|
||||
|
||||
const gfxAttributes: IGFXInputAttribute[] = [];
|
||||
@@ -464,20 +489,27 @@ export class Mesh extends Asset {
|
||||
});
|
||||
});
|
||||
|
||||
const referedVertexBuffers = primitive.vertexBundelIndices.map(
|
||||
const vbReference = primitive.vertexBundelIndices.map(
|
||||
(i) => vertexBuffers[i]);
|
||||
|
||||
const inputAssemblerInfo: IGFXInputAssemblerInfo = {
|
||||
attributes: gfxAttributes,
|
||||
vertexBuffers: referedVertexBuffers,
|
||||
vertexBuffers: vbReference,
|
||||
};
|
||||
if (indexBuffer) {
|
||||
inputAssemblerInfo.indexBuffer = indexBuffer;
|
||||
}
|
||||
|
||||
const geomInfo: any = primitive.geomInfo;
|
||||
if (geomInfo) {
|
||||
geomInfo.indices = ib;
|
||||
geomInfo.positions = new Float32Array(buffer, geomInfo.range.offset, geomInfo.range.length / 4);
|
||||
}
|
||||
|
||||
return {
|
||||
primitiveMode: primitive.primitiveMode,
|
||||
inputAssembler: gfxDevice.createInputAssembler(inputAssemblerInfo),
|
||||
geometricInfo: geomInfo,
|
||||
} as IRenderingSubmesh ;
|
||||
});
|
||||
|
||||
|
||||
+11
-1
@@ -1,4 +1,4 @@
|
||||
import { GFXAttributeName } from '../../gfx/define';
|
||||
import { GFXAttributeName, GFXPrimitiveMode } from '../../gfx/define';
|
||||
export { default as find } from '../../scene-graph/find';
|
||||
import Texture2D from '../../assets/texture-2d';
|
||||
import { Filter, PixelFormat, WrapMode } from '../../assets/texture-base';
|
||||
@@ -115,6 +115,16 @@ export function createMesh (geometry: IGeometry) {
|
||||
bufferBlob.addBuffer(indexBuffer);
|
||||
}
|
||||
|
||||
// geometric info for raycasting
|
||||
if (geometry.primitiveMode >= GFXPrimitiveMode.TRIANGLE_LIST) {
|
||||
const geomInfo = Float32Array.from(geometry.positions);
|
||||
primitive.geomInfo = {
|
||||
doubleSided: geometry.doubleSided,
|
||||
range: { offset: bufferBlob.getLength(), length: geomInfo.byteLength },
|
||||
};
|
||||
bufferBlob.addBuffer(geomInfo.buffer);
|
||||
}
|
||||
|
||||
// Create mesh struct.
|
||||
const meshStruct: IMeshStruct = {
|
||||
vertexBundles: [vertexBundle],
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { Vec3 } from '../../core/value-types';
|
||||
import { vec3 } from '../../core/vmath';
|
||||
import { GFXPrimitiveMode } from '../../gfx/define';
|
||||
import { IGeometry, IGeometryOptions } from './defines';
|
||||
import { IGeometry, IGeometryOptions } from './define';
|
||||
|
||||
interface IBoxOptions extends IGeometryOptions {
|
||||
/**
|
||||
|
||||
@@ -60,4 +60,9 @@ export interface IGeometry {
|
||||
* Vertex colors.
|
||||
*/
|
||||
colors?: number[];
|
||||
|
||||
/**
|
||||
* whether rays casting from the back face of this geometry could collide with it
|
||||
*/
|
||||
doubleSided?: boolean;
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
import { IGeometry } from './defines';
|
||||
import { IGeometry } from './define';
|
||||
|
||||
export function translate (geometry: IGeometry, offset: { x?: number; y?: number; z?: number; }) {
|
||||
const x = offset.x || 0;
|
||||
|
||||
+4
-3
@@ -279,16 +279,17 @@ export enum GFXPrimitiveMode {
|
||||
LINE_LIST,
|
||||
LINE_STRIP,
|
||||
LINE_LOOP,
|
||||
LINE_LIST_ADJACENCY,
|
||||
LINE_STRIP_ADJACENCY,
|
||||
ISO_LINE_LIST,
|
||||
// raycast detectable:
|
||||
TRIANGLE_LIST,
|
||||
TRIANGLE_STRIP,
|
||||
TRIANGLE_FAN,
|
||||
LINE_LIST_ADJACENCY,
|
||||
LINE_STRIP_ADJACENCY,
|
||||
TRIANGLE_LIST_ADJACENCY,
|
||||
TRIANGLE_STRIP_ADJACENCY,
|
||||
TRIANGLE_PATCH_ADJACENCY,
|
||||
QUAD_PATCH_LIST,
|
||||
ISO_LINE_LIST,
|
||||
}
|
||||
|
||||
export enum GFXPolygonMode {
|
||||
|
||||
@@ -118,7 +118,7 @@ export abstract class GFXInputAssembler extends GFXObject {
|
||||
public abstract initialize (info: IGFXInputAssemblerInfo): boolean;
|
||||
public abstract destroy (): void;
|
||||
|
||||
public getVertexBuffer (stream: number): GFXBuffer | null {
|
||||
public getVertexBuffer (stream: number = 0): GFXBuffer | null {
|
||||
if (stream < this._vertexBuffers.length) {
|
||||
return this._vertexBuffers[stream];
|
||||
} else {
|
||||
|
||||
@@ -78,6 +78,9 @@ const WebGLPrimitives: GLenum[] = [
|
||||
WebGLRenderingContext.LINES,
|
||||
WebGLRenderingContext.LINE_STRIP,
|
||||
WebGLRenderingContext.LINE_LOOP,
|
||||
WebGLRenderingContext.NONE,
|
||||
WebGLRenderingContext.NONE,
|
||||
WebGLRenderingContext.NONE,
|
||||
WebGLRenderingContext.TRIANGLES,
|
||||
WebGLRenderingContext.TRIANGLE_STRIP,
|
||||
WebGLRenderingContext.TRIANGLE_FAN,
|
||||
@@ -85,9 +88,6 @@ const WebGLPrimitives: GLenum[] = [
|
||||
WebGLRenderingContext.NONE,
|
||||
WebGLRenderingContext.NONE,
|
||||
WebGLRenderingContext.NONE,
|
||||
WebGLRenderingContext.NONE,
|
||||
WebGLRenderingContext.NONE,
|
||||
WebGLRenderingContext.NONE,
|
||||
];
|
||||
|
||||
const WebGLWraps: GLenum[] = [
|
||||
|
||||
@@ -159,6 +159,7 @@ export class Model {
|
||||
set subMeshData (sm: IRenderingSubmesh | null) {
|
||||
this._subMeshObject = sm;
|
||||
}
|
||||
|
||||
get subMeshData () {
|
||||
return this._subMeshObject;
|
||||
}
|
||||
|
||||
@@ -164,42 +164,37 @@ export class RenderScene {
|
||||
public raycast (worldRay: ray, mask: number = Layers.RaycastMask): IRaycastResult[] {
|
||||
pool.reset();
|
||||
for (const m of this._models) {
|
||||
const node = m.node;
|
||||
if (!cc.Layers.check(node.layer, mask) || !m.modelBounds) { continue; }
|
||||
// transform ray back to model space
|
||||
mat4.invert(m4, node.getWorldMatrix(m4));
|
||||
vec3.transformMat4(modelRay.o, worldRay.o, m4);
|
||||
vec3.normalize(modelRay.d, vec3.transformMat4Normal(modelRay.d, worldRay.d, m4));
|
||||
// broadphase
|
||||
distance = intersect.ray_aabb(modelRay, m.modelBounds);
|
||||
if (distance <= 0) { continue; }
|
||||
const subModel = m.subMeshData;
|
||||
if (!subModel || subModel.primitiveMode !== GFXPrimitiveMode.TRIANGLE_LIST) { continue; }
|
||||
const vbuffer = subModel.inputAssembler.getVertexBuffer(0);
|
||||
const ibuffer = subModel.inputAssembler.indexBuffer;
|
||||
if (!vbuffer || !vbuffer.buffer || !ibuffer || !ibuffer.buffer) { continue; }
|
||||
// narrowphase
|
||||
distance = Infinity;
|
||||
const vb = new Float32Array(vbuffer.buffer as ArrayBuffer);
|
||||
const ib = new Uint16Array(ibuffer.buffer as ArrayBuffer);
|
||||
const sides = subModel.doubleSided;
|
||||
for (let j = 0; j < ib.length; j += 3) {
|
||||
const i0 = ib[j] * 3;
|
||||
const i1 = ib[j + 1] * 3;
|
||||
const i2 = ib[j + 2] * 3;
|
||||
vec3.set(tri.a, vb[i0], vb[i0 + 1], vb[i0 + 2]);
|
||||
vec3.set(tri.b, vb[i1], vb[i1 + 1], vb[i1 + 2]);
|
||||
vec3.set(tri.c, vb[i2], vb[i2 + 1], vb[i2 + 2]);
|
||||
const dist = intersect.ray_triangle(modelRay, tri, sides);
|
||||
if (dist <= 0 || dist > distance) { continue; }
|
||||
distance = dist;
|
||||
}
|
||||
if (distance < Infinity) {
|
||||
const r = pool.add();
|
||||
r.node = node;
|
||||
r.distance = distance * vec3.magnitude(vec3.mul(v3, modelRay.d, node._scale));
|
||||
results[pool.length - 1] = r;
|
||||
}
|
||||
const node = m.node;
|
||||
if (!cc.Layers.check(node.layer, mask) || !m.modelBounds) { continue; }
|
||||
// transform ray back to model space
|
||||
mat4.invert(m4, node.getWorldMatrix(m4));
|
||||
vec3.transformMat4(modelRay.o, worldRay.o, m4);
|
||||
vec3.normalize(modelRay.d, vec3.transformMat4Normal(modelRay.d, worldRay.d, m4));
|
||||
// broadphase
|
||||
distance = intersect.ray_aabb(modelRay, m.modelBounds);
|
||||
if (distance <= 0) { continue; }
|
||||
const subModel = m.subMeshData;
|
||||
if (!subModel || !subModel.geometricInfo) { continue; }
|
||||
const { positions: vb, indices: ib, doubleSided: sides } = subModel.geometricInfo;
|
||||
// narrowphase
|
||||
distance = Infinity;
|
||||
for (let j = 0; j < ib.length; j += 3) {
|
||||
const i0 = ib[j] * 3;
|
||||
const i1 = ib[j + 1] * 3;
|
||||
const i2 = ib[j + 2] * 3;
|
||||
vec3.set(tri.a, vb[i0], vb[i0 + 1], vb[i0 + 2]);
|
||||
vec3.set(tri.b, vb[i1], vb[i1 + 1], vb[i1 + 2]);
|
||||
vec3.set(tri.c, vb[i2], vb[i2 + 1], vb[i2 + 2]);
|
||||
const dist = intersect.ray_triangle(modelRay, tri, sides);
|
||||
if (dist <= 0 || dist > distance) { continue; }
|
||||
distance = dist;
|
||||
}
|
||||
if (distance < Infinity) {
|
||||
const r = pool.add();
|
||||
r.node = node;
|
||||
r.distance = distance * vec3.magnitude(vec3.mul(v3, modelRay.d, node._scale));
|
||||
results[pool.length - 1] = r;
|
||||
}
|
||||
}
|
||||
results.length = pool.length;
|
||||
return results;
|
||||
|
||||
Reference in New Issue
Block a user